import wx
from PanelLayout import PanelLayout

class TodoListPanel(wx.Panel):
	"""docstring para esta clase"""
	
	def __init__(self, parent, id, todoList):
		"""docstring for __init__"""
		self.todoList = todoList
		self.parent = parent
		self.recentlyQuit = False
		self.todoListItemsPanels = []
		
		self.PL1 = PanelLayout(parent, title=todoList.title, expand=1) 
		self.PL1.txt.Bind(wx.EVT_LEFT_DCLICK, self.changeName)
		self.PL1.txt.Bind(wx.EVT_LEFT_DOWN, self.setFocusOnList)
		self.PL1.txtControl.Bind(wx.EVT_KILL_FOCUS, self.returnToTitle)
		self.PL1.txtControl.Bind(wx.EVT_KEY_DOWN, self.parseEnter)
		    
		self.hbox = wx.BoxSizer(wx.VERTICAL)
		
		self.separator = wx.StaticText(self.PL1, -1, size=(-1,5))
		self.hbox.Add(self.separator, 0)
		
		for item in todoList.items:
			todoListItem = TodoListItemPanel(self.PL1, -1, item)
			self.hbox.Add(todoListItem, 1, wx.EXPAND | wx.ALL, 3)
			
			self.todoListItemsPanels.append(todoListItem)
		    
		#END PANEL 1
		self.PL1.FinishLayout()
	
	def changeName(self, event):
		self.PL1.hbox.Detach(self.PL1.txt)
		self.PL1.hbox.Insert(0, self.PL1.txtControl, 1, wx.EXPAND)
		self.PL1.txt.Show(0)
		self.PL1.txtControl.Show(1)
		self.PL1.refreshEverything()
		self.PL1.txtControl.SetFocus()
		
		if event != None:
			event.Skip()
	
	def setFocusOnList(self, event):
		"""docstring para setFocusOnList"""
		self.parent.parent.updateTodoListFocus(self.todoList.id)
		self.PL1.txt.SetBackgroundColour("#6699FF")
		self.PL1.Refresh()	
	
	def resetHeading(self):
		self.PL1.txt.SetBackgroundColour("white")
		self.PL1.Refresh()
	
	def parseEnter(self, event):
		self.recentlyQuit = False
		if event.GetKeyCode() == 13:
			self.returnToTitle(event)
		
		event.Skip()
	
	def returnToTitle(self, event):
		if self.recentlyQuit:
			self.recentlyQuit = False
			return
			
		self.recentlyQuit = True
		
		# le pongo el nuevo valor al label
		newTitle = self.PL1.txtControl.GetValue()
		self.todoList.title = newTitle
		self.PL1.txt.SetLabel(self.todoList.title)
		
		# reemplazo todo
		self.PL1.hbox.Detach(self.PL1.txtControl)
		self.PL1.hbox.Insert(0, self.PL1.txt, 1, wx.EXPAND)
		self.PL1.txt.Show(1)
		self.PL1.txtControl.Show(0)
		self.PL1.refreshEverything()
		
		event.Skip()
	
	def resetItemsColors(self, itemId):
		"""docstring for resetItemsColors"""
		for item in self.todoListItemsPanels:
			if item.todoListItem.id != itemId:
				item.backToBlack()
	
	def addItem(self, item):
		self.todoList.items.append(item)
		todoListItem = TodoListItemPanel(self.PL1, -1, item, created=True)
		self.hbox.Add(todoListItem, 1, wx.EXPAND | wx.ALL, 3)
		self.PL1.refreshEverything()
		todoListItem.headingTxtCtrl.SetFocus()
	
	def onSetFocus(self, event):
		"""docstring for onSetFocus"""
		print "epaaa cambio el focou"
	


class TodoListItemPanel(wx.Panel):
	"""docstring for TodoListItem"""
	
	def __init__(self, parent, id, todoListItem, created = False):
		"""docstring for __init__"""
		wx.Panel.__init__(self, parent, id, (0, 0), (640, 25))
		self.parent = parent
		self.itemSizer = wx.FlexGridSizer(rows=1, cols=3, hgap=4)
		self.SetSizer(self.itemSizer)
		self.todoListItem = todoListItem
		self.recentlyQuit = False
		
		font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
		font.SetPointSize(10)
		
		jpg1 = wx.Image("./resources/action.jpg", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
		self.type = wx.StaticBitmap(self, -1, jpg1, (10 + jpg1.GetWidth(), 5), (jpg1.GetWidth(), jpg1.GetHeight()))
		self.itemSizer.Add(self.type, 0, wx.TOP, 3)
		
		self.heading = wx.StaticText(self, 1, todoListItem.title)
		self.heading.SetFont(font)
		self.headingTxtCtrl = wx.TextCtrl(self, 1, todoListItem.title)
		
		if created == True:
			self.itemSizer.Add(self.headingTxtCtrl, -1, 
							   wx.EXPAND | wx.LEFT | wx.TOP, 2)
			
			self.heading.Show(0)
		else:
			self.itemSizer.Add(self.heading, -1, 
							   wx.EXPAND | wx.LEFT | wx.TOP, 2)
			self.headingTxtCtrl.Show(0)
			
		self.headingTxtCtrl.Bind(wx.EVT_KILL_FOCUS, self.returnToTitle)
		self.headingTxtCtrl.Bind(wx.EVT_KEY_DOWN, self.parseEnter)
		
		self.viewButton = wx.Button(self, -1, "Ver", size=(50, 20))
		self.itemSizer.Add(self.viewButton, 1, wx.ALIGN_RIGHT | wx.RIGHT, 15)
		
		self.itemSizer.AddGrowableCol(1, 1)
		
		self.heading.Bind(wx.EVT_LEFT_DOWN, self.changeHeadingColor)
		self.heading.Bind(wx.EVT_LEFT_DCLICK, self.convertLabelToText)
	
	def changeHeadingColor(self, event):
		"""docstring for onSetFocus"""
		self.parent.parent.parent.updateItemFocus(self.todoListItem.id)
		self.heading.SetForegroundColour('red')
		self.Refresh()
	
	def backToBlack(self):
		"""docstring for backToBlack"""
		self.heading.SetForegroundColour('black')
		self.Refresh()
				
	def convertLabelToText(self, event):
		self.itemSizer.Detach(self.heading)
		self.itemSizer.Insert(1, self.headingTxtCtrl, 1, 
							  wx.EXPAND | wx.LEFT | wx.TOP, 2)
		
		self.heading.Show(0)
		self.headingTxtCtrl.Show(1)
		self.Layout()
		self.Refresh()
		
		if event != None:
			event.Skip()
	
	def parseEnter(self, event):
		self.recentlyQuit = False
		if event.GetKeyCode() == 13:
			self.returnToTitle(event)
		
		event.Skip()
	
	def returnToTitle(self, event):
		if self.recentlyQuit:
			self.recentlyQuit = False
			return
			
		self.recentlyQuit = True
		
		# le pongo el nuevo valor al label
		newTitle = self.headingTxtCtrl.GetValue()
		self.todoListItem.title = newTitle
		self.heading.SetLabel(self.todoListItem.title)
		
		# reemplazo todo
		self.itemSizer.Detach(self.headingTxtCtrl)
		self.itemSizer.Insert(1, self.heading, 1, 
							  wx.EXPAND | wx.TOP | wx.LEFT, 2)
		
		self.heading.Show(1)
		self.headingTxtCtrl.Show(0)
		self.Layout()
		self.Refresh()
		
		event.Skip()
		

